import { getStore, setStore } from "@/util/store";
import { isURL, validatenull } from "@/util/validate";
import {
  loginByMobile,
  loginBySocial,
  loginByUsername,
  getUserInfo,
  logout,
  refreshToken,
} from "@/api/login";
import { deepClone } from "@/util";
import { aesEncrypt } from "@/util/encryption";
import webiste from "@/const/website";
import { resetRouter } from "@/router/router";
import { getMenu, getTopMenu } from "@/api/admin/menu";

function addPath(ele, first) {
  const menu = webiste.menu;
  const propsConfig = menu.props;
  const propsDefault = {
    label: propsConfig.label || "name",
    path: propsConfig.path || "path",
    icon: propsConfig.icon || "icon",
    children: propsConfig.children || "children",
  };
  const icon = ele[propsDefault.icon];
  ele[propsDefault.icon] = validatenull(icon) ? menu.iconDefault : icon;
  const isChild =
    ele[propsDefault.children] && ele[propsDefault.children].length !== 0;
  if (!isChild) ele[propsDefault.children] = [];
  if (!isChild && first && !isURL(ele[propsDefault.path])) {
    ele[propsDefault.path] = ele[propsDefault.path] + "/index";
  } else {
    ele[propsDefault.children].forEach((child) => {
      addPath(child);
    });
  }
}

const user = {
  state: {
    userInfo:
      getStore({
        name: "userInfo",
      }) || {},
    permissions:
      getStore({
        name: "permissions",
      }) || [],
    roles: [],
    menu:
      getStore({
        name: "menu",
      }) || [],
    menuAll: [],
    access_token:
      getStore({
        name: "access_token",
      }) || "",
    refresh_token:
      getStore({
        name: "refresh_token",
      }) || "",
  },
  actions: {
    // 根据用户名登录
    LoginByUsername({ commit }, userInfo) {
      if (webiste.passwordEnc) {
        userInfo.password = aesEncrypt(userInfo.password, webiste.encPassword);
      }
      return new Promise((resolve, reject) => {
        loginByUsername(
          userInfo.username,
          userInfo.password,
          userInfo.code,
          userInfo.randomStr
        )
          .then((response) => {
            const data = response.data.data;
            commit("SET_ACCESS_TOKEN", data.accessToken);
            commit("SET_REFRESH_TOKEN", data.refreshToken);
            commit("CLEAR_LOCK");
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 根据手机号登录
    LoginByPhone({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        loginByMobile(userInfo.mobile, userInfo.code)
          .then((response) => {
            const data = response.data;
            commit("SET_ACCESS_TOKEN", data.access_token);
            commit("SET_REFRESH_TOKEN", data.refresh_token);
            commit("CLEAR_LOCK");
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 根据OpenId登录
    LoginBySocial({ commit }, param) {
      return new Promise((resolve, reject) => {
        loginBySocial(param.state, param.code)
          .then((response) => {
            const data = response.data;
            commit("SET_ACCESS_TOKEN", data.access_token);
            commit("SET_REFRESH_TOKEN", data.refresh_token);
            commit("CLEAR_LOCK");
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 刷新token
    RefreshToken({ commit, state }) {
      return new Promise((resolve, reject) => {
        refreshToken(state.refresh_token)
          .then((response) => {
            const data = response.data;
            commit("SET_ACCESS_TOKEN", data.access_token);
            commit("SET_REFRESH_TOKEN", data.refresh_token);
            commit("CLEAR_LOCK");
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 查询用户信息
    GetUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then((res) => {
            // const data = res.data.data || {};

            const data = {
              sysUser: {
                userId: "1856898062528065537",
                username: "tester001",
                password:
                  "$2a$10$pPNcCrUf000OXftDh4ezU.QbIJmgVY7INVQUvuzm0tD6GQ3l7IJeO",
                createBy: "zhoumin",
                updateBy: null,
                createTime: "2024-11-14 11:12:52",
                updateTime: null,
                delFlag: "0",
                lockFlag: "0",
                phone: "12345678912",
                avatar: null,
                deptId: "1",
                tenantId: "1",
                wxOpenid: null,
                miniOpenid: null,
                qqOpenid: null,
                giteeLogin: null,
                oscId: null,
                nickname: null,
                name: "QQQ",
                email: null,
                ddUserid: null,
                fsOpenid: null,
              },
              permissions: [
                "sys_role_export",
                "workorder_trainrecordmanage_edit",
                "pfoc_customercomplaintrecordledger_export",
                "workorder_workorderschedulemain_edit",
                "workorder_shiftmanage_view",
                "pfoc_workposition_view",
                "pfoc_pfocprocess_del",
                "workorder_cpktemp_view",
                "sys_user_edit",
                "sys_menu_del",
                "generator_customercomplaintrecordledgercopy_add",
                "workorder_personmanage_add",
                "workorder_workordercps_view",
                "workorder_personcredentialmanage_edit",
                "pfoc_productproductionrelease_add",
                "pfoc_badcategory_add",
                "workorder_personcredentialmanage_del",
                "pfoc_customercomplaintrecordledger_del",
                "pfoc_filelibrary_add",
                "sys_dict_del",
                "workorder_workorderreportscrapdetails_export",
                "workorder_personmanage_edit",
                "pfoc_fqcrecord_del",
                "pfoc_scraplibrary_edit",
                "workorder_workorderreport_view",
                "pfoc_realityproducelinemodeling_add",
                "workorder_workorderreportscrapdetails_view",
                "sys_menu_edit",
                "pfoc_workposition_add",
                "workorder_shiftmanage_del",
                "sys_user_add",
                "workorder_workorderbom_view",
                "workorder_hdviewapiitem_view",
                "workorder_personcredentialmanage_export",
                "sys_log_del",
                "pfoc_badcategory_view",
                "pfoc_realityproductlinemain_del",
                "pfoc_customercomplaintrecordledger_edit",
                "pfoc_realityproductlinemain_edit",
                "pfoc_realityproducelinemodeling_del",
                "workorder_hdviewapiitem_add",
                "jimu_edit_btn",
                "workorder_trainrecordmanage_export",
                "workorder_trainrecordmanage_del",
                "sys_dict_edit",
                "workorder_shiftmanage_export",
                "pfoc_filelibrary_view",
                "pfoc_factorysettings_edit",
                "pfoc_workshop_del",
                "pfoc_fqcrecord_edit",
                "workorder_workorderproductionreport_edit",
                "jimu_view_btn",
                "pfoc_productproductionrelease_release",
                "workorder_cpktemp_add",
                "workorder_workordercps_del",
                "admin_syspublicparam_del",
                "workorder_workorderschedulemain_add",
                "pfoc_workshop_edit",
                "workorder_workordercps_export",
                "workorder_workordercps_edit",
                "generator_customercomplaintrecordledgercopy_export",
                "pfoc_scraplibrary_del",
                "workorder_personmanage_del",
                "pfoc_productproductionrelease_del",
                "pfoc_customercomplaintrecordledger_view",
                "workorder_shiftmanage_add",
                "admin_syspublicparam_edit",
                "pfoc_badcategory_export",
                "pfoc_fqcrecord_export",
                "pfoc_realityproducelinemodeling_view",
                "pfoc_pfocprocess_view",
                "workorder_workorderreport_edit",
                "workorder_workorderreportdetails_export",
                "workorder_shiftmanage_edit",
                "sys_role_edit",
                "pfoc_workshop_export",
                "pfoc_badcategory_edit",
                "workorder_personmanage_view",
                "workorder_hdviewapiitem_edit",
                "pfoc_workposition_del",
                "sys_role_del",
                "workorder_workorderschedulemain_view",
                "workorder_workorderproductionreport_add",
                "jimu_add_btn",
                "sys_user_del",
                "sys_menu_add",
                "pfoc_filelibrary_del",
                "workorder_hdviewapiitem_export",
                "pfoc_productproductionrelease_view",
                "workorder_cpktemp_edit",
                "pfoc_scraplibrary_export",
                "pfoc_realityproductlinemain_view",
                "workorder_workorderbom_export",
                "workorder_personmanage_export",
                "pfoc_customercomplaintrecordledger_add",
                "sys_user_export",
                "workorder_personcredentialmanage_view",
                "generator_customercomplaintrecordledgercopy_del",
                "pfoc_fqcrecord_add",
                "pfoc_badcategory_del",
                "workorder_trainrecordmanage_view",
                "workorder_cpktemp_del",
                "pfoc_productproductionrelease_edit",
                "pfoc_scraplibrary_view",
                "workorder_workorderschedulemain_del",
                "pfoc_workshop_add",
                "jimu_del_btn",
                "workorder_workordercps_add",
                "admin_syspublicparam_add",
                "pfoc_realityproductlinemain_add",
                "workorder_workorderreportdetails_view",
                "workorder_workorderreport_export",
                "sys_log_export",
                "pfoc_workshop_view",
                "workorder_trainrecordmanage_add",
                "workorder_workorderproductionreport_del",
                "pfoc_pfocprocess_edit",
                "sys_dict_add",
                "sys_role_add",
                "pfoc_fqcrecord_view",
                "pfoc_realityproducelinemodeling_edit",
                "generator_customercomplaintrecordledgercopy_edit",
                "pfoc_pfocprocess_add",
                "pfoc_scraplibrary_add",
                "pfoc_realityproducelinemodeling_export",
                "pfoc_filelibrary_edit",
                "sys_role_perm",
                "pfoc_workposition_edit",
                "workorder_personcredentialmanage_add",
                "workorder_workorderproductionreport_view",
                "jimu_copy_btn",
                "workorder_hdviewapiitem_del",
              ],
              roles: ["1"],
            };

            commit("SET_USER_INFO", data.sysUser);
            commit("SET_ROLES", data.roles || []);
            commit("SET_PERMISSIONS", data.permissions || []);
            resolve(data);
          })
          .catch(() => {
            reject();
          });
      });
    },
    // 登出
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        logout()
          .then(() => {
            resetRouter();
            commit("SET_MENU", []);
            commit("SET_PERMISSIONS", []);
            commit("SET_USER_INFO", {});
            commit("SET_ACCESS_TOKEN", "");
            commit("SET_REFRESH_TOKEN", "");
            commit("SET_ROLES", []);
            commit("DEL_ALL_TAG");
            commit("CLEAR_LOCK");
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 注销session
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        resetRouter();
        commit("SET_MENU", []);
        commit("SET_PERMISSIONS", []);
        commit("SET_USER_INFO", {});
        commit("SET_ACCESS_TOKEN", "");
        commit("SET_REFRESH_TOKEN", "");
        commit("SET_ROLES", []);
        commit("DEL_ALL_TAG");
        commit("CLEAR_LOCK");
        resolve();
      });
    },

    // 获取系统菜单
    GetMenu({ commit }, obj) {
      let this_ = this;
      // 记录用户点击顶部信息，保证刷新的时候不丢失
      commit("LIKE_TOP_MENUID", obj);

      console.log("getMenu-obj", obj);

      return new Promise((resolve) => {
        getMenu(obj.id).then((res) => {
          const data = res.data.data.menus;

          const menuList = data.filter((item) => {
            return item.systemCode == 2 || item.systemCode == 6;

          });

          console.log(123, menuList);

          let menus = menuList.map((item) => {
            return {
              ...item,
              visible: item.visible ? "1" : "0",
              children: item.children.map((i) => {
                // console.log('原始component值:', i.component);
                function extractWujieParams(str) {
                  // 兼容两种写法：带 <url ...> 或不带
                  const url = str.includes("<url")
                    ? str.replace(/<url[^>]*>/g, "").replace(/<\/url>/g, "")
                    : str;

                  const u = new URL(url, "http://fake-host"); // 补一个 base，避免解析失败
                  return {
                    sub: u.searchParams.get("sub") || "",
                    host: u.searchParams.get("host") || "",
                  };
                }

                // console.log(1, extractWujieParams(i.component).sub);

                return {
                  ...i,
                  path: extractWujieParams(i.component).sub,
                };
              }),
            };
          });

          // console.log("menu", menus);

          const menuData = [
            {
              //               component: "",
              // componentName: "",
              // icon: "icon-msnui-supervise",
              // id: "1917387889152888834",
              // keepAlive: "0",
              // name: "基础信息",
              // label: "基础信息",
              // // parentId: 0,
              // path: "/basic",
              // visible: "1",
              component: "",
              componentName: "",
              icon: "ep:connection",
              id: 2824,
              keepAlive: true,
              name: "基础信息",
              parentId: 0,
              path: "/basic",
              visible: "1",
              children: [
                {
                  id: "1917388491001958401",
                  parentId: "1917387889152888834",
                  weight: 10,
                  name: "工厂大屏",
                  path: "/workorder/factoryLargeScreen/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 10,
                  icon: null,
                  menuType: "0",
                  permission: null,
                  label: "工厂大屏",
                },
                {
                  id: "1917388677442965505",
                  parentId: "1917387889152888834",
                  weight: 20,
                  name: "车间大屏",
                  path: "/workorder/workshopLargeScreen/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 20,
                  icon: null,
                  menuType: "0",
                  permission: null,
                  label: "车间大屏",
                },
                {
                  id: "1917388841008238594",
                  parentId: "1917387889152888834",
                  weight: 30,
                  name: "产线大屏",
                  path: "/workorder/productionLineLargeScreen/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 30,
                  icon: null,
                  menuType: "0",
                  permission: null,
                  label: "产线大屏",
                },
              ],
            },
            {
              id: "1780608102510333953",
              parentId: "-1",
              weight: 22,
              name: "数据中心",
              path: "/data",
              keepAlive: "0",
              visible: "1",
              sortOrder: 22,
              icon: "icon-canshu",
              menuType: "0",
              permission: null,
              label: "数据中心",
              children: [
                {
                  id: "1864938846824124417",
                  parentId: "1780608102510333953",
                  weight: 30,
                  name: "数据查询",
                  path: "/inspectdata",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 30,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "数据查询",
                  children: [
                    {
                      id: "1869362996443488258",
                      parentId: "1864938846824124417",
                      weight: 10,
                      name: "报工数据明细",
                      path: "/workorder/workorderreport/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 10,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "报工数据明细",
                    },
                    {
                      id: "1927186363166232577",
                      parentId: "1864938846824124417",
                      weight: 11,
                      name: "检测数据明细",
                      path: "/workorder/reportdatadetails/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 11,
                      icon: "",
                      menuType: "0",
                      permission: null,
                      label: "检测数据明细",
                    },
                    {
                      id: "1927270076453490689",
                      parentId: "1864938846824124417",
                      weight: 12,
                      name: "不良数据明细",
                      path: "/workorder/workorderreportdetails/index-page",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 12,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "不良数据明细",
                    },
                    {
                      id: "1927279751857180673",
                      parentId: "1864938846824124417",
                      weight: 13,
                      name: "报废数据明细",
                      path: "/workorder/workorderreportscrapdetails/index-page",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 13,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "报废数据明细",
                    },
                    {
                      id: "1927285793382993922",
                      parentId: "1864938846824124417",
                      weight: 15,
                      name: "物料数据明细",
                      path: "/workorder/workorderbom/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 15,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "物料数据明细",
                    },
                    {
                      id: "1865224103058083841",
                      parentId: "1864938846824124417",
                      weight: 20,
                      name: "插回损-HS",
                      path: "/workorder/opltshhs/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 20,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "插回损-HS",
                    },
                    {
                      id: "1865225802757836802",
                      parentId: "1864938846824124417",
                      weight: 30,
                      name: "干涉-多芯(MTPro)",
                      path: "/workorder/interferometermtpro/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 30,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "干涉-多芯(MTPro)",
                    },
                    {
                      id: "1882371298546917377",
                      parentId: "1864938846824124417",
                      weight: 40,
                      name: "产品数据追溯",
                      path: "/workorder/workorderproductionreport/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 40,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "产品数据追溯",
                    },
                  ],
                },
                {
                  id: "1865216340852609025",
                  parentId: "1780608102510333953",
                  weight: 40,
                  name: "数据分析",
                  path: "/cpkdata",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 40,
                  icon: null,
                  menuType: "0",
                  permission: null,
                  label: "数据分析",
                  children: [
                    {
                      id: "1865216469881982978",
                      parentId: "1865216340852609025",
                      weight: 1,
                      name: "综合CPK",
                      path: "/workorder/cpktemp/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 1,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "综合CPK",
                    },
                    {
                      id: "1873991743596728321",
                      parentId: "1865216340852609025",
                      weight: 90,
                      name: "直方图",
                      path: "/zhifangtu",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 90,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "直方图",
                      children: [
                        {
                          id: "1873634416569229314",
                          parentId: "1873991743596728321",
                          weight: 4,
                          name: "DR8插回损直方图",
                          path: "/workorder/ilrlview/DR8-LIRL-Histogram",
                          keepAlive: "1",
                          visible: "1",
                          sortOrder: 4,
                          icon: "icon-liuliang",
                          menuType: "0",
                          permission: null,
                          label: "DR8插回损直方图",
                        },
                        {
                          id: "1873674381734846465",
                          parentId: "1873991743596728321",
                          weight: 5,
                          name: "GEN1.5插回损直方图",
                          path: "/workorder/ilrlview/GEN1.5-ILRL-Histogram",
                          keepAlive: "1",
                          visible: "1",
                          sortOrder: 5,
                          icon: "icon-liuliang",
                          menuType: "0",
                          permission: null,
                          label: "GEN1.5插回损直方图",
                        },
                        {
                          id: "1873665373724614658",
                          parentId: "1873991743596728321",
                          weight: 6,
                          name: "SR8 GEN2插回损直方图",
                          path: "/workorder/ilrlview/SR8-LIRL-Histogram",
                          keepAlive: "1",
                          visible: "1",
                          sortOrder: 6,
                          icon: "icon-liuliang",
                          menuType: "0",
                          permission: null,
                          label: "SR8 GEN2插回损直方图",
                        },
                      ],
                    },
                  ],
                },
                {
                  id: "1873991935700045826",
                  parentId: "1780608102510333953",
                  weight: 50,
                  name: "实时监控",
                  path: "/realtimeData",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 50,
                  icon: null,
                  menuType: "0",
                  permission: null,
                  label: "实时监控",
                  children: [
                    {
                      id: "1839952043406757889",
                      parentId: "1873991935700045826",
                      weight: 1,
                      name: "DR8-插回损",
                      path: "/workorder/ilrlview/DR8-ILRL",
                      keepAlive: "1",
                      visible: "1",
                      sortOrder: 1,
                      icon: "icon-liuliang",
                      menuType: "0",
                      permission: null,
                      label: "DR8-插回损",
                    },
                    {
                      id: "1839952273774710786",
                      parentId: "1873991935700045826",
                      weight: 2,
                      name: "GEN1.5-插回损",
                      path: "/workorder/ilrlview/GEN1.5-ILRL",
                      keepAlive: "1",
                      visible: "1",
                      sortOrder: 2,
                      icon: "icon-liuliang",
                      menuType: "0",
                      permission: null,
                      label: "GEN1.5-插回损",
                    },
                    {
                      id: "1839952366611435522",
                      parentId: "1873991935700045826",
                      weight: 3,
                      name: "SR8 GEN2-插回损",
                      path: "/workorder/ilrlview/SR8-ILRL",
                      keepAlive: "1",
                      visible: "1",
                      sortOrder: 3,
                      icon: "icon-liuliang",
                      menuType: "0",
                      permission: null,
                      label: "SR8 GEN2-插回损",
                    },
                  ],
                },
                {
                  id: "1920727414059065345",
                  parentId: "1780608102510333953",
                  weight: 60,
                  name: "测量数据",
                  path: "/Measurement data recording",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 60,
                  icon: null,
                  menuType: "0",
                  permission: null,
                  label: "测量数据",
                  children: [
                    {
                      id: "1912067498031951873",
                      parentId: "1920727414059065345",
                      weight: 10,
                      name: "尺寸测量记录",
                      path: "/workorder/measure/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 10,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "尺寸测量记录",
                    },
                    {
                      id: "1912049298170949633",
                      parentId: "1920727414059065345",
                      weight: 20,
                      name: "干涉记录",
                      path: "/workorder/interferometermtpro/summaryAlone",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 20,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "干涉记录",
                    },
                    {
                      id: "1912060414154485762",
                      parentId: "1920727414059065345",
                      weight: 30,
                      name: "极性记录",
                      path: "/workorder/polarityproduction/indexAlone",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 30,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "极性记录",
                    },
                    {
                      id: "1912060743214411778",
                      parentId: "1920727414059065345",
                      weight: 40,
                      name: "插回损记录",
                      path: "/workorder/opltshhs/summaryAlone",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 40,
                      icon: null,
                      menuType: "0",
                      permission: null,
                      label: "插回损记录",
                    },
                  ],
                },
                {
                  id: "1920753042640625666",
                  parentId: "1780608102510333953",
                  weight: 100,
                  name: "质量维护记录",
                  path: "/Data Record",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 100,
                  icon: null,
                  menuType: "0",
                  permission: null,
                  label: "质量维护记录",
                  children: [
                    {
                      id: "1745292397961",
                      parentId: "1920753042640625666",
                      weight: 10,
                      name: "客诉记录台账",
                      path: "/pfoc/customercomplaintrecordledger/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 10,
                      icon: "",
                      menuType: "0",
                      permission: "",
                      label: "客诉记录台账",
                    },
                    {
                      id: "1745566905423",
                      parentId: "1920753042640625666",
                      weight: 20,
                      name: "FQC记录",
                      path: "/pfoc/fqcrecord/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 20,
                      icon: "",
                      menuType: "0",
                      permission: "",
                      label: "FQC记录",
                    },
                    {
                      id: "1919684194976411650",
                      parentId: "1920753042640625666",
                      weight: 30,
                      name: "关键工序不良记录",
                      path: "/pfoc/workorderfailrecord/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 30,
                      icon: "",
                      menuType: "0",
                      permission: null,
                      label: "关键工序不良记录",
                    },
                    {
                      id: "1932996454474944514",
                      parentId: "1920753042640625666",
                      weight: 999,
                      name: "客诉记录追溯",
                      path: "/workorder/customercomplaintrecordledger/index",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 999,
                      icon: "",
                      menuType: "0",
                      permission: null,
                      label: "客诉记录追溯",
                    },
                  ],
                },
                {
                  id: "1927632966534062082",
                  parentId: "1780608102510333953",
                  weight: 999,
                  name: "其他数据",
                  path: "/otherData",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 999,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "其他数据",
                  children: [
                    {
                      id: "1920027271163355138",
                      parentId: "1927632966534062082",
                      weight: 6,
                      name: "东蘅2-3",
                      path: "surveillance",
                      keepAlive: "0",
                      visible: "1",
                      sortOrder: 6,
                      icon: "",
                      menuType: "0",
                      permission: null,
                      label: "东蘅2-3",
                      children: [
                        {
                          id: "1920030205984972801",
                          parentId: "1920027271163355138",
                          weight: 111,
                          name: "直方图",
                          path: "/workorder/newilrlview/DR8-LIRL-Histogram",
                          keepAlive: "0",
                          visible: "1",
                          sortOrder: 111,
                          icon: "",
                          menuType: "0",
                          permission: null,
                          label: "直方图",
                        },
                        {
                          id: "1920031133995704322",
                          parentId: "1920027271163355138",
                          weight: 112,
                          name: "实时监控",
                          path: "/workorder/newilrlview/DR8-ILRL",
                          keepAlive: "0",
                          visible: "1",
                          sortOrder: 112,
                          icon: "",
                          menuType: "0",
                          permission: null,
                          label: "实时监控",
                        },
                        {
                          id: "1920031447649951746",
                          parentId: "1920027271163355138",
                          weight: 113,
                          name: "插回损记录",
                          path: "/workorder/newOpltshhs/summaryAlone",
                          keepAlive: "0",
                          visible: "1",
                          sortOrder: 113,
                          icon: "",
                          menuType: "0",
                          permission: null,
                          label: "插回损记录",
                        },
                        {
                          id: "1940231001886322690",
                          parentId: "1920027271163355138",
                          weight: 114,
                          name: "干涉记录",
                          path: "/workorder/interferometermtpro/summaryAlone2-3",
                          keepAlive: "0",
                          visible: "1",
                          sortOrder: 114,
                          icon: null,
                          menuType: "0",
                          permission: null,
                          label: "干涉记录",
                        },
                        {
                          id: "1940309524235866114",
                          parentId: "1920027271163355138",
                          weight: 115,
                          name: "极性记录",
                          path: "/workorder/polarityproduction/indexAlone2-3",
                          keepAlive: "0",
                          visible: "1",
                          sortOrder: 115,
                          icon: null,
                          menuType: "0",
                          permission: null,
                          label: "极性记录",
                        },
                      ],
                    },
                  ],
                },
              ],
            },
            {
              id: "1792850001417674753",
              parentId: "-1",
              weight: 33,
              name: "工单管理",
              path: "/workorder",
              keepAlive: "0",
              visible: "1",
              sortOrder: 33,
              icon: "icon-caidanguanli",
              menuType: "0",
              permission: null,
              label: "工单管理",
              children: [
                {
                  id: "1716278082247",
                  parentId: "1792850001417674753",
                  weight: 0,
                  name: "工单管理",
                  path: "/workorder/workordercps/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 0,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "工单管理",
                },
                {
                  id: "1935995263413411841",
                  parentId: "1792850001417674753",
                  weight: 2,
                  name: "工单排产",
                  path: "/workorder/workorderschedulemain/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 2,
                  icon: null,
                  menuType: "0",
                  permission: null,
                  label: "工单排产",
                },
                {
                  id: "1890258791120732161",
                  parentId: "1792850001417674753",
                  weight: 4,
                  name: "排产统计",
                  path: "/workorder/workorderschedulemain/summary",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 4,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "排产统计",
                },
              ],
            },
            {
              id: "1725413983996420098",
              parentId: "-1",
              weight: 44,
              name: "产线管理",
              path: "/line",
              keepAlive: "0",
              visible: "1",
              sortOrder: 44,
              icon: "icon-guiji",
              menuType: "0",
              permission: null,
              label: "产线管理",
              children: [
                {
                  id: "1731931088716",
                  parentId: "1725413983996420098",
                  weight: 2,
                  name: "工位库",
                  path: "/pfoc/workposition/workPosition",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 2,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "工位库",
                },
                {
                  id: "1722410408891",
                  parentId: "1725413983996420098",
                  weight: 3,
                  name: "产线建模",
                  path: "/pfoc/realityproducelinemodeling/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 3,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "产线建模",
                },
                {
                  id: "1933454530810765314",
                  parentId: "1725413983996420098",
                  weight: 3,
                  name: "产线列表",
                  path: "/pfoc/realityproduceline/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 3,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "产线列表",
                },
              ],
            },
            {
              id: "1780528761122295810",
              parentId: "-1",
              weight: 55,
              name: "工艺管理",
              path: "/release",
              keepAlive: "0",
              visible: "1",
              sortOrder: 55,
              icon: "icon-dingdan",
              menuType: "0",
              permission: null,
              label: "工艺管理",
              children: [
                {
                  id: "1742969891579",
                  parentId: "1780528761122295810",
                  weight: 0,
                  name: "产品信息",
                  path: "/workorder/hdviewapiitem/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 0,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "产品信息",
                },
                {
                  id: "1849790391084634114",
                  parentId: "1780528761122295810",
                  weight: 1,
                  name: "产品类型",
                  path: "/admin/dict/product-type",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 1,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "产品类型",
                },
                {
                  id: "1692614676793",
                  parentId: "1780528761122295810",
                  weight: 11,
                  name: "工序库",
                  path: "/pfoc/pfocprocess/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 11,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "工序库",
                },
                {
                  id: "1713941926632",
                  parentId: "1780528761122295810",
                  weight: 22,
                  name: "生产工艺",
                  path: "/pfoc/productproductionrelease/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 22,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "生产工艺",
                },
                {
                  id: "1743145170161",
                  parentId: "1780528761122295810",
                  weight: 33,
                  name: "文件库",
                  path: "/pfoc/filelibrary/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 33,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "文件库",
                },
                {
                  id: "1743061402689",
                  parentId: "1780528761122295810",
                  weight: 44,
                  name: "不良库",
                  path: "/pfoc/badcategory/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 44,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "不良库",
                },
                {
                  id: "1926933700050755585",
                  parentId: "1780528761122295810",
                  weight: 55,
                  name: "报废库",
                  path: "/pfoc/scraplibrary/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 55,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "报废库",
                },
              ],
            },
            {
              id: "1849277282855149570",
              parentId: "-1",
              weight: 66,
              name: "员工管理",
              path: "/staff",
              keepAlive: "0",
              visible: "1",
              sortOrder: 66,
              icon: "icon-sucaisads",
              menuType: "0",
              permission: null,
              label: "员工管理",
              children: [
                {
                  id: "1721200712690",
                  parentId: "1849277282855149570",
                  weight: 1,
                  name: "员工信息",
                  path: "/workorder/personcredentialmanage/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 1,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "员工信息",
                },
                {
                  id: "1721292233504",
                  parentId: "1849277282855149570",
                  weight: 2,
                  name: "培训记录",
                  path: "/workorder/trainrecordmanage/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 2,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "培训记录",
                },
              ],
            },
            {
              id: "1927636804464828418",
              parentId: "-1",
              weight: 77,
              name: "工厂管理",
              path: "/factoryManagement",
              keepAlive: "0",
              visible: "1",
              sortOrder: 77,
              icon: "icon-record",
              menuType: "0",
              permission: null,
              label: "工厂管理",
              children: [
                {
                  id: "1711675747012",
                  parentId: "1927636804464828418",
                  weight: 10,
                  name: "工厂设置",
                  path: "/pfoc/factorysettings/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 10,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "工厂设置",
                },
                {
                  id: "1720750264572",
                  parentId: "1927636804464828418",
                  weight: 20,
                  name: "车间设置",
                  path: "/pfoc/workshop/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 20,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "车间设置",
                },
              ],
            },
            {
              id: "1849277855654469634",
              parentId: "-1",
              weight: 80,
              name: "班组管理",
              path: "/banzu",
              keepAlive: "0",
              visible: "1",
              sortOrder: 80,
              icon: "icon-yonghuguanli",
              menuType: "0",
              permission: null,
              label: "班组管理",
              children: [
                {
                  id: "1720775689635",
                  parentId: "1849277855654469634",
                  weight: 1,
                  name: "班次管理",
                  path: "/workorder/shiftmanage/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 1,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "班次管理",
                },
                {
                  id: "1720775666789",
                  parentId: "1849277855654469634",
                  weight: 2,
                  name: "人员岗位",
                  path: "/workorder/personmanage/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 2,
                  icon: "",
                  menuType: "0",
                  permission: "",
                  label: "人员岗位",
                },
              ],
            },
            {
              id: "2000",
              parentId: "-1",
              weight: 88,
              name: "配置中心",
              path: "/system",
              keepAlive: "0",
              visible: "1",
              sortOrder: 88,
              icon: "icon-xitongguanli",
              menuType: "0",
              permission: null,
              label: "配置中心",
              children: [
                {
                  id: "1100",
                  parentId: "2000",
                  weight: 1,
                  name: "用户管理",
                  path: "/admin/user/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 1,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "用户管理",
                },
                {
                  id: "1300",
                  parentId: "2000",
                  weight: 2,
                  name: "角色管理",
                  path: "/admin/role/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 2,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "角色管理",
                },
                {
                  id: "2200",
                  parentId: "2000",
                  weight: 3,
                  name: "字典管理",
                  path: "/admin/dict/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 3,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "字典管理",
                },
                {
                  id: "2100",
                  parentId: "2000",
                  weight: 4,
                  name: "日志管理",
                  path: "/admin/log/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 4,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "日志管理",
                },
                {
                  id: "1200",
                  parentId: "2000",
                  weight: 5,
                  name: "菜单管理",
                  path: "/admin/menu/index",
                  keepAlive: "0",
                  visible: "1",
                  sortOrder: 5,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "菜单管理",
                },
                {
                  id: "2210",
                  parentId: "2000",
                  weight: 7,
                  name: "参数管理",
                  path: "/admin/param/index",
                  keepAlive: "1",
                  visible: "1",
                  sortOrder: 7,
                  icon: "",
                  menuType: "0",
                  permission: null,
                  label: "参数管理",
                },
              ],
            },
            {
              id: "7000",
              parentId: "-1",
              weight: 88,
              name: "自定义报表设计",
              path: "/report/bi/index",
              keepAlive: "0",
              visible: "1",
              sortOrder: 88,
              icon: "icon-icon-p_mrpbaobiao",
              menuType: "0",
              permission: null,
              label: "自定义报表设计",
            },
          ];

          console.log(456, menuData);

          // const menu = deepClone(data);
          const menu = deepClone(menus);
          menu.forEach((ele) => {
            addPath(ele);
          });
          const type = obj.type;
          commit("SET_MENU", { type, menu });
          resolve(menu);
        });
      });
    },
    //顶部菜单
    GetTopMenu() {
      return new Promise((resolve) => {
        getTopMenu().then((res) => {
          const data = res.data.data || [];
          resolve(data);
        });
      });
    },
  },
  mutations: {
    SET_ACCESS_TOKEN: (state, access_token) => {
      state.access_token = access_token;
      setStore({
        name: "access_token",
        content: state.access_token,
        type: "session",
      });
    },
    SET_REFRESH_TOKEN: (state, rfToken) => {
      state.refresh_token = rfToken;
      setStore({
        name: "refresh_token",
        content: state.refresh_token,
        type: "session",
      });
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo;
      setStore({
        name: "userInfo",
        content: userInfo,
        type: "session",
      });
    },
    SET_MENU: (state, params = {}) => {
      const { menu, type } = params;
      if (type !== false) state.menu = menu;
      setStore({
        name: "menu",
        content: menu,
        type: "session",
      });
    },
    SET_MENU_ALL: (state, menuAll) => {
      state.menuAll = menuAll;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_PERMISSIONS: (state, permissions) => {
      const list = {};
      for (let i = 0; i < permissions.length; i++) {
        list[permissions[i]] = true;
      }

      state.permissions = list;
      setStore({
        name: "permissions",
        content: list,
        type: "session",
      });
    },
  },
};
export default user;
